<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class ActivityController extends CommonController {
	
	function index(){
		$this->display('index');
	}
	
	/**
	 * [research_list 问卷调查列表]
	 * author  [lzm]
	 * time    2016年7月5日
 	 * @return [array] [description]
	 */
	public function research_list(){
		$research = M('research');
		$count = $research->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list = $research->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display('index');
	}
	//添加问卷调查页面
	public function add_research_index(){
		//选择所有奖品列表
		$arr=M('award')->where('endtime>"'.time().'"')->select();
		//echo M()->_sql();exit('@@@');
		$this->assign('arr',$arr);
		$this->display('add_research');
	}
	//添加问卷调查内容
	public function add_research(){
		$research=M('research');
		//接收参数
		$state=I('state');
		$img=html_entity_decode(I('cover'));
		if ($state==1) {
			$arr=$research->where('state=1')->find();
			//print_r($arr);exit;
			if (!empty($arr)) {
				//exit('123'); 
			   error('只能开启一个问卷调查',$_SERVER['HTTP_REFERER']);
			}
		}
		$aid=serialize(I('checkbox'));
		//print_r($aid);exit;
		$name=trim(I('title'));
		$description=trim(I('description'));
		$ctime=date('Y-m-d H:i:s',time());
		$mtime=date('Y-m-d H:i:s',time());
		$access=I('access');
		$data=array(
			'aid'=>$aid,
			'name'=>$name,
			'img'=>$img,
			'description'=>$description,
			'ctime'=>$ctime,
			'mtime'=>$mtime,
			'state'=>$state,
			'access'=>$access,
			);
		//print_r($data);exit('@#$%');
		$res_add=$research->data($data)->add();
		if (false !== $res_add) {
			 $this->redirect('Activity/research_list');
		}else{
			  error('操作失败',$_SERVER['HTTP_REFERER']);
		}
		
	}
	//删除问卷调查
	public function del_research(){
		$id=I('id');
		$research=M('research');
		//删除调查表数据
		$res_del=$research->where('id='.$id)->delete();
		//删除问题表数据
		$result_del=M('question')->where('rid="'.$id.'"')->delete();
		//echo M()->_sql();exit;
		if (false !== $res_del && false !== $result_del) {
		    $this->redirect('Activity/research_list');
		}else{
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}
	}

	//修改内容显示
	public function update_index(){
		$research=M('research');
		$id=I('id');
		$list=$research->where('id="'.$id.'"')->find();
		//查出对应的奖品
		$aid=unserialize($list['aid']);
		$arr=M('award')->where('endtime>"'.time().'"')->select();
		$this->assign('v',$arr);
		$this->assign('arr',$aid);
		$this->assign('list',$list);
		$this->display('update_research');
	}
	//修改内容
	public function update_research(){
		$research=M('research');
		//接收参数
		$state=I('state');
		$img=html_entity_decode(I('cover'));
		if ($state==1) {
			$arr=$research->where('state=1')->count();
			//print_r($arr);exit;
			if ($arr>=1) {
				 error('只能开启一个问卷调查',$_SERVER['HTTP_REFERER']);
			}
		}
		$id=I('id');

		$aid=serialize(I("checkbox"));
		$access=I('access');
		$name=trim(I('title'));
		$description=trim(I('description'));
		$mtime=date('Y-m-d H:i:s',time());
		$data=array(
			'name'=>$name,
			'description'=>$description,
			'img'=>$img,
			'mtime'=>$mtime,
			'state'=>$state,
			'aid'=>$aid,
			'access'=>$access,
			);
		//print_r($data);exit;
		$res_save=$research->where('id="'.$id.'"')->data($data)->save();
		if (false !== $res_save) {
			 $this->redirect('Activity/research_list');
		}else{
			 error('操作失败',$_SERVER['HTTP_REFERER']);
			 //$this->redirect('Activity/research_list');
		}
	}
	

	//问题应答首页
	public function question(){
		$id=I('id');
		//print_r($id);exit;
		$question=M('question');
		$list=$question->where('rid="'.$id.'"')->select();
		$this->assign('id',$id);
		$this->assign('list',$list);
		$this->display('question');
		//echo 'hello';
	}
	//添加问题首页
	public function add_question(){
		$id=I('id');
		//print_r($id);exit;
		$this->assign('id',$id);
		$this->display('add_question');
	}
	//添加问题到数据库
	public function question_add(){
		$rid=I('id');
		//print_r($rid);exit;
		$type=I('type');
		$question=trim(I('question'));
		$checkbox=serialize(I('name'));

		$data=array(
			'rid'=>$rid,
			'name'=>$question,
			'type'=>$type,
			'checkbox'=>$checkbox,
			'ctime'=>date("Y-m-d H:i:s",time()),
			'mtime'=>date("Y-m-d H:i:s",time()),
			);
		$res_add=M('question')->data($data)->add();
		if (false == $res_add){
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}else{
			$this->redirect('Activity/question',array('id' => $rid));

		}
	}


	//删除问题
	public function del_question(){
		$id=I('id');
		$res=M('question')->where('id="'.$id.'"')->find();
		$rid=$res['rid'];
		$res_del=M('question')->where('id="'.$id.'"')->delete();
		if (false !== $res_del) {
		    $this->redirect('Activity/question',array('id' => $rid));
		}else{
			exit('fail');
		    $this->redirect('Activity/question');
		}
	}


	//修改问题页面
	public function update_question_index(){
		$id=I('id');
		$list=M('question')->where('id="'.$id.'"')->find();
		$name=unserialize($list['checkbox']);
		foreach ($name as $value) {
			 $arr[]=$value;
		}
		$this->assign('name',$name);
		$this->assign('list',$list);
		$this->display('update_question');

	}
	//修改问题
	public function question_update(){
		$id=I('id');
		$rid=I('rid');
		$type=I('type');
		$question=trim(I('question'));
		$checkbox=serialize(I('name'));
		$data=array(
			'name'=>$question,
			'type'=>$type,
			'checkbox'=>$checkbox,
			'mtime'=>date("Y-m-d H:i:s",time()),
			);
		//print_r($data);exit;
		$res_save=M('question')->where('id="'.$id.'"')->data($data)->save();
		//echo M()->_sql();exit;
		if (false == $res_save){
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}else{
			$this->redirect('Activity/question',array('id' => $rid));
		}
	}

	//测试A方法
	public function testA(){
		$User=A('Home/Menu');
		$list=$User->menu();
		print_r($list);exit('@@');
	}





	
	public function exportcsv() {
		

		//1、首先查开启的问卷调查
		$research=M('research')->where('state=1')->find();

		//2、查找问题
		$question=M('question')->where('rid="'.$research['id'].'"')->select();
		foreach ($question as $key => $value) {
			$arr[]=$value['name'];
			$question[$key]['check']=implode(';',unserialize($value['checkbox']));
		}
		$q=implode(",", $arr);//所有的问题
		
		//P($question);exit;

		//去回答记录表找回答此问卷调查的人
		$people=M('answer')->where('r_name="'.$research['name'].'"')->select();

		$result = array();

		$str = "问题,选项"."\r\n";
		foreach($people as $key=>$value){
			//标题
			$title=$research['name'];
			//问题$q
			
			//答案
			$arr_answer=$value['answer'];
			$name = $value['name'];
			$phone = $value['phone'];
			$ctime = date('Y-m-d H:i:s',$value['ctime']);
			$str .= "\"".$title."\",\"".$q."\",\"".$arr_answer."\",\"".$name."\",\"".$phone."\",".$ctime."\r\n";
			$result[] = array(
				'title'	=>	$title,
				'answer'	=>	$arr_answer,
				'name'	=>	$name,
				'phone'	=>	$phone
			);

		}

		$sql = ' drop table if exists `sy_tmp`; CREATE TABLE `sy_tmp` (   `f1` varchar(50) ,`f2` varchar(50) ,`f3` varchar(50) ,`f4` varchar(50) ,`f5` varchar(50) ,`f6` varchar(50) ,`f7` varchar(50) ,`f8` varchar(50) ,`f9` varchar(50) ,`f10` varchar(50),`f11` varchar(50),`f12` varchar(50),`f13` varchar(50),`f14` varchar(50),`f15` varchar(50),`f16` varchar(50),`f17` varchar(50),`f18` varchar(50),`f19` varchar(50),`f20` varchar(50),`f21` varchar(50),`f22` varchar(50),`f23` varchar(50),`f24` varchar(50),`f25` varchar(50) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;';

		$rs = M('Apt')->qu($sql);

		$aslist = array();

		$cCount = 0;

		foreach($result as $r){
			$tar = explode(',',$r['answer']);
			if($cCount==0){
				$cCount = count($tar);
			}
			$l = array();
			foreach($tar as $k=>$v){
				$l['f'.($k+1)] = $v;
			}
			M('Tmp')->data($l)->add();
		}


		$ctArr = array();
		for($i=1;$i<=$cCount;$i++){
			$sql="select `f".$i."`,count(*) as ct from sy_tmp group by `f".$i."`";
			$query_c=M('apt')->query($sql);
			$ctArr[] = 	$query_c;			
		} 
		$vals = array();
		foreach ($ctArr as $key => $value) {
			$tar = array();
			foreach($value as $v){
				$tar[] = implode(':',$v); 
			}
			$vals[]=implode('|',$tar);
		} 
		//P($vals);
		//问卷调查标题
		$this->assign('title',$research['name']);
		//问题以及选项
		$this->assign('question',$question);
		$this->assign('vals',$vals);
		$this->display('result');
	}
		 	


	
}